<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>OR Manager</title>
<?php $this->load->helper('HTML');
echo link_tag('css/style2.css');
echo link_tag('images/favicon.ico','shortcut icon','image/x-icon');
?></head>
<?php

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=or_managerall.xls");
header("Pragma: no-cache");
header("Expires: 0");

?>
<body>
               <table width="90%" border="1" cellpadding="1" cellspacing="1" id="detail" align="center">
                    <thead>
                        <tr class="detail2">
                        <th rowspan="2">NO</th>
                        <th rowspan="2">DEALER</th>
                        <th rowspan="2">Unit</th>
                        <th rowspan="2">PRIBADI</th>
                        <th colspan="2">UNIT</th>
                        <th rowspan="2">Harga <br />Dealer</th>
                        <th rowspan="2">Overiding</th>
                        <th rowspan="2">OR <br /> Baby manager</th>
                        <th rowspan="2">Bonus Plus</th>
                        <th rowspan="2">Total</th>
                        <th rowspan="2">PPH 5%</th>
                        <th rowspan="2">Terima</th>
                        <th rowspan="2">Kota</th>
                       </tr>
					   <tr><th>GROUP 20%</th><th>GROUP 10%</th></tr>
                   </thead>
				   <?php 
				    $no=1;
                    foreach($data_or_manager as $row){
						$cekweek_rekrut = array();
						$cekpribadi	=	array();
						$cekpenjualanrekrut = array();
						
						$pr = 0;
						$pp = 0;
						$pg10 = 0;
						$pg20 = 0;
						$hargadealer = 0;
						$ord = 0;
						$totalbaby = 0;
						$totalgroup = 0;
						$terima =0;
						
						$cek = 0;
						$cek1 = 0;
						$cek2	=	0;
						$nominal = 0;
						$i = 0;
						$z=0;
						$penjualangroup10	=	"";
						$penjualangroup20	=	"";
						//total omset
						$nominal = $this->Cm_model->rekrut_penjualan_week($row->strkode_dealer, $bulan);
						foreach($nominal as $sow){
							$cekpenjualanrekrut[$i++] = $sow->inttotal_omset;
						}
						//hitung weeks
						for($i=0;$i<count($weeks);$i++){
							//hitung omset pribadi per weeknya
							$week_pribadi = $this->Cm_model->weeks_pribadi_or($row->intid_dealer, $weeks[$i]->intid_week);
							if(!empty($week_pribadi[0]->total)) {
							   $pp =  $pp + $week_pribadi[0]->total;
							   $cekpribadi[$i]	=  $week_pribadi[0]->total;
								}else {
									$pp = $pp + 0;
									$cekpribadi[$i]	=	0;
								}
							//hitung rekrut pribadi per weeknya
							$week_rekrut = $this->Cm_model->week_rekrut($row->strkode_dealer, $weeks[$i]->intid_week);
							if(!empty($week_rekrut[0]->total)) {
									$cekweek_rekrut[$i]	=	$week_rekrut[0]->total;
								}else {
									$cekweek_rekrut[$i]	=	0;
								};
							//hitung perjualan rekrut anaknya yang memiliki omset
							$penjualanrekrut = $this->Cm_model->penjualanrekrut_or2($row->strkode_dealer, $weeks[$i]->intid_week,$bulan);
								if(!empty($penjualanrekrut[0]->total)) {
									$pr = $pr + $penjualanrekrut[0]->total;
								}else {
									$pr= $pr;
								}
								
							}
						//kondisi nya jika setiap 2 minggu min 1 rekrut dalam 1 bulan
						if($cekweek_rekrut[0] > 0 and  $cekweek_rekrut[1] > 0 and $cekweek_rekrut[2] > 0 and  $cekweek_rekrut[3] > 0){
								$cek = 1;
							}elseif($cekweek_rekrut[0] > 0 or $cekweek_rekrut[2] > 0){
								$cek = 1;
							}elseif($cekweek_rekrut[0] > 0 or $cekweek_rekrut[3] > 0){
								$cek = 1;
							}elseif($cekweek_rekrut[1] > 0 or $cekweek_rekrut[2] > 0){
								$cek = 1;
							}elseif($cekweek_rekrut[1] > 0 or $cekweek_rekrut[3] > 0){
								$cek = 1;
							}else{
								$cek = 0;
							}
							//kondisi ny jika penjualan pribadi setiap minggu melebihi 300000
							if($cekpribadi[0] >= 300000 and $cekpribadi[1] >= 300000 and $cekpribadi[2] >= 300000 and $cekpribadi[3] >= 300000){
								$cek1 = 1;
							}else{
								$cek1 = 0;								
							}
							for($i=0;$i <sizeof($cekpenjualanrekrut);$i++){
								if($cekpenjualanrekrut[$i] >= 150000){
									$z = 1 + $z;
								}
							}
							if($z >= 2){
								$cek2 = 1;
							}else{
								$cek2 = 0;
							}
							
						///ditambahkan saja
						if($cek == 1 and $cek1 == 1 and $cek2 == 1){
							$penjualangroup10 = $this->Cm_model->get_penjualan_group($row->intid_unit,10,$bulan);
							$penjualangroup20 = $this->Cm_model->get_penjualan_group($row->intid_unit,20,$bulan);
							$baby_omset = $this->Cm_model->get_penjualan_baby_manager($row->intid_unit,$bulan);
							foreach($baby_omset as $rod){
								if(!empty($rod->inttotal_omset)){
									$totalbaby = $totalbaby + $rod->inttotal_omset;
									}
								}
							$pg10 = $penjualangroup10[0]->intomset10;
							$pg20 = $penjualangroup20[0]->intomset20;
							$hargadealer	=	($pg10 * 0.9) + ($pg20 * 0.8);
							$ord = ($hargadealer * 0.03);
							$totalbaby = $totalbaby * 0.03;
							
							$totalgroup = $pg10 + $pg20;
							  if ($totalgroup>6000000 and $totalgroup<=7999999) 
							  {
								$bonusplus = 120000;
								  } else if ($totalgroup>8000000 and $totalgroup<=9999999) 
								  {
									$bonusplus = 200000;
								  } else if ($totalgroup>10000000 and $totalgroup<=14999999)  
								  {
									$bonusplus = 275000;
								  } else if ($totalgroup>15000000 and $totalgroup<=19999999)
								  {
									$bonusplus = 450000;
								  } else if ($totalgroup>20000000 and $totalgroup<=24999999) 
								  {
									$bonusplus = 650000;
								  } else if ($totalgroup>25000000 and $totalgroup<=29999999)
								  {
									$bonusplus = 875000;
								  } else if ($totalgroup>30000000 and $totalgroup<=34999999)
								  {
									$bonusplus = 1125000;
								  } else {
									$bonusplus = 0;
								  }
							
							$total = $bonusplus + $ord + $totalbaby;
							$pph = 0.05 * $total;
							$terima = $total - $pph;
							echo "<tr>
							<td>".$no++."</td>";
							echo "<td>".$row->strnama_dealer."</td>";
							echo "<td>".$row->strnama_unit."</td>";
							echo "<td>".$pp."</td>";
							echo "<td>".$pg10."</td>";
							echo "<td>".$pg20."</td>";
							echo "<td>".$hargadealer."</td>";
							echo "<td>".$ord."</td>";
							echo "<td>".$totalbaby."</td>";
							echo "<td>".$bonusplus."</td>";
							echo "<td>".$total."</td>";
							echo "<td>".$pph."</td>";
							echo "<td>".$terima."</td>";
							echo "<td>".$row->strnama_cabang."</td>
							</tr>";
							}
						}
						?>
				   </table>
</body>
</html>
